<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="add"
          >
            添加面经
          </el-button>
        </div>
      </template>
      <el-table :data="list" style="width: 100%" stripe>
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="stem" label="标题"> </el-table-column>
        <el-table-column prop="creator" label="作者"> </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览量"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
        <el-table-column prop="" label="操作">
          <template #default="scope">
            <el-button
              type="text"
              icon="el-icon-view"
              @click="preview(scope.row.id)"
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-edit-outline"
              @click="changeForm(scope.row.id)"
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="delForm(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page="1"
        @current-change="changeCurrent"
      >
      </el-pagination>
    </div>
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      direction="rtl"
      size="50%"
      @close="clearForm"
    >
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        :rules="rules"
        v-if="title !== '预览面经'"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          ></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="seveAdd">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
      <div v-else>
        <h3>{{ form.stem }}</h3>
        <el-divider></el-divider>
        <div v-html="form.content"></div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import {
  getArticleList,
  addArticleList,
  delArticleList,
  changeArticleList,
  gainArticleList
} from '@/api/articel'
export default {
  name: 'article-page',
  data() {
    return {
      params: {
        current: 1,
        pageSize: 10
      },
      form: {
        stem: '',
        content: ''
      },
      list: [],
      total: null,
      drawer: false,
      title: '',
      rules: {
        stem: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    quillEditor
  },
  created() {
    this.getArticleList()
  },
  methods: {
    async getArticleList() {
      const res = await getArticleList(this.params)
      this.list = res.data.rows
      this.total = res.data.total
    },
    changeCurrent(val) {
      this.params.current = val
      this.getArticleList()
    },
    add() {
      this.drawer = true
      this.title = '添加面经'
    },
    async seveAdd() {
      try {
        await this.$refs.form.validate()
        if (this.title === '修改面经') {
          const form = {}
          form.id = this.form.id
          form.stem = this.form.stem
          form.content = this.form.content
          await changeArticleList(form)
          this.drawer = false
        } else {
          await addArticleList(this.form)
          this.drawer = false
        }
        this.getArticleList()
        this.$message.success('添加成功')
      } catch (error) {
        console.log(error)
      }
    },
    clearForm() {
      this.$refs.form.resetFields()
    },
    async delForm(id) {
      try {
        await delArticleList(id)
        this.$message.success('删除成功')
        this.getArticleList()
      } catch (error) {
        console.log(error)
      }
    },
    async changeForm(id) {
      this.drawer = true
      this.title = '修改面经'
      const res = await gainArticleList(id)
      console.log(res)
      this.form = res.data
    },
    async preview(id) {
      this.title = '预览面经'
      const res = await gainArticleList(id)
      console.log(res)
      this.form.stem = res.data.stem
      this.form.content = res.data.content
      this.drawer = true
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
